package com.ujiuye.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BaseDao<T> {
    //创建QueryRunner对象
    private QueryRunner runner = new QueryRunner(new ComboPooledDataSource());

    //创建传参数增删改查
    public int jUpdate(String sql, Object... params) {
        try {
            return runner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //查询单条记录
    public T jSelect_One(String sql, Class<T> type, Object... params) {
        try {
            return runner.query(sql, new BeanHandler<T>(type), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询多条记录
    public List<T> jSelect_Multi(String sql, Class<T> type, Object... params) {
        try {
            return runner.query(sql, new BeanListHandler<>(type), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
//查询单行单列
    public Object jscale(String sql,Object...params){
        try {
            return runner.query(sql,new ScalarHandler<>(),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
